package com.zf.yichat.controller;

import com.zf.yichat.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
public abstract class BaseController {
//
//    @Value("${rsa_key.private_path}")
//    private String PRIVATE_KEY_PATH;

    protected HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

    protected SysUser getUser() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return (SysUser) session.getAttribute("user");
    }

    protected boolean isOperate() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return (Boolean) session.getAttribute("isOperate");
    }

    protected Integer getUserId() {
        return getUser().getId();
    }


    /**
     *    解析密码
     */
    protected String passwordDecrypt(String password) {
//        try {
//            RSAPrivateKey sharePrivateKey = RsaUtilWeb.getPrivateKey(PRIVATE_KEY_PATH);
//            return RsaUtilWeb.webDecrypt(sharePrivateKey, password);
//        } catch (Exception e) {
//            log.error("解析密码错误", e);
//            return null;
//        }
        return null;
    }
}
